package com.osgi.desktop.ui.plugins;

import com.osgi.desktop.domain.SitePlugin;
import com.osgi.desktop.domain.SiteUpdate;
import com.osgi.desktop.ui.util.UtilIcons;
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;

public class PluginsDisponiveisTreeCellRenderer extends DefaultTreeCellRenderer {
    private static final long serialVersionUID = 1L;
    
    public PluginsDisponiveisTreeCellRenderer() {}
    
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        DefaultMutableTreeTableNode node = (DefaultMutableTreeTableNode)value;
        Object userObject = node.getUserObject();
        if (userObject instanceof SiteUpdate) {
            SiteUpdate siteUpdate = (SiteUpdate)userObject;
            setIcon(UtilIcons.pluginsSiteUpdateIconPeq());
            setText(siteUpdate.getNome());
            setToolTipText(siteUpdate.getUrl());
        } else if (userObject instanceof SitePlugin) {
            SitePlugin sitePlugin = (SitePlugin)userObject;
            setIcon(UtilIcons.pluginsSitePluginIconPeq());
            setText("");
            String descricao = sitePlugin.getDescricao();
            setToolTipText(isValidDescricao(descricao) ? descricao : "Sem descrição");
        }
        return this;
    }
    
    public boolean isValidDescricao(String descricao) {
        return descricao != null && descricao.trim().length() > 0;
    }
}
